package com.me.study.javaBasic.collection;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.cglib.beans.BeanMap;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 自定义 TreeMap 排序规则，并拼接成字符串
 *
 * @author ME
 * @date 2020/11/27
 */
public class TestTreeMap {

    /**
     * 使用 TreeMap 自动排序
     */
    @Test
    public void sort() {
        // Cglib（spring内置源码） 对象转 Map
        Request request = new Request("1000010001", "UTF8", "P2020040115282833353", "MD5", "ibuaiVcKdpRxkhJA");
        BeanMap beanMap = BeanMap.create(request);

        // 方式一：排序再输出
        // List<String> keys = new ArrayList<String>(beanMap.keySet());
        // Collections.sort(keys); //按照参数key有序排列
        // StringJoiner joiner = new StringJoiner("&");
        // for (String key : keys) {
        //     if (StringUtils.isBlank((String) beanMap.get(key))) {
        //         continue;
        //     }
        //     joiner.add(key + "=" + beanMap.get(key));
        // }

        // 方式二：排序再输出
        TreeMap<String, String> map = new TreeMap<>(beanMap);
        StringJoiner joiner = new StringJoiner("&");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (StringUtils.isBlank(entry.getValue())) {
                continue;
            }
            joiner.add(entry.toString());
        }

        System.out.println("StringJoiner 线程非安全的:");
        System.out.println(joiner.toString());

        // ==================== Stream 拼接字符串 ======================
        Map<String, String> treeMap = new TreeMap<>();
        // 内部类重写 compare 对 key排序
        // Map<String, String> treeMap = new TreeMap<>(new Comparator<String>() {
        //     @Override
        //     public int compare(String str1, String str2) {
        //         // -1 升序 1 降序
        //         return str1.compareTo(str2);
        //     }
        // });

        treeMap.put("mch_id", "1000010001");
        treeMap.put("charset", "UTF8");
        treeMap.put("out_trade_no", "P2020040115282833353");
        treeMap.put("sign_type", "MD5");
        treeMap.put("nonce_str", "ibuaiVcKdpRxkhJA");

        System.out.println("\nentrySet().stream():");
        String collect = treeMap.entrySet().stream().map(Map.Entry::toString).collect(Collectors.joining("&"));
        System.out.println(collect);
    }

    public static class Request {
        private String mch_id;
        private String charset;
        private String out_trade_no;
        private String sign_type;
        private String nonce_str;
        private String sign;

        public Request(String mch_id, String charset, String out_trade_no, String sign_type, String nonce_str) {
            this.mch_id = mch_id;
            this.charset = charset;
            this.out_trade_no = out_trade_no;
            this.sign_type = sign_type;
            this.nonce_str = nonce_str;
        }

        public String getMch_id() {
            return mch_id;
        }

        public void setMch_id(String mch_id) {
            this.mch_id = mch_id;
        }

        public String getCharset() {
            return charset;
        }

        public void setCharset(String charset) {
            this.charset = charset;
        }

        public String getOut_trade_no() {
            return out_trade_no;
        }

        public void setOut_trade_no(String out_trade_no) {
            this.out_trade_no = out_trade_no;
        }

        public String getSign_type() {
            return sign_type;
        }

        public void setSign_type(String sign_type) {
            this.sign_type = sign_type;
        }

        public String getNonce_str() {
            return nonce_str;
        }

        public void setNonce_str(String nonce_str) {
            this.nonce_str = nonce_str;
        }

        public String getSign() {
            return sign;
        }

        public void setSign(String sign) {
            this.sign = sign;
        }
    }

}
